<template>
    <base-dialog v-bind="$attrs" v-on="$listeners" :title="title" @submit="toAdd" :disabled="disabled" size="medium">
        <el-form ref="elForm" label-width="auto" :model="formData" :rules="rules" :disabled="disabled">
            <el-form-item label="消息标题" prop="title">
                <el-input v-model="formData.title"></el-input>
            </el-form-item>
            <el-form-item label="目标类型" prop="type" v-if="!disabled">
              <dict-radio v-model="formData.type" dist-key="notice_type" is-button></dict-radio>
            </el-form-item>
          <el-form-item label="目标用户" prop="user" v-if="formData.type==1&&!disabled">
            <user-select-search v-model="formData.user" multiple placeholder="请选择目标用户" class="full-width"></user-select-search>
          </el-form-item>
          <el-form-item label="目标角色" prop="role" v-if="formData.type==2">
            <role-select v-model="formData.role" multiple filterable></role-select>
          </el-form-item>
          <el-form-item label="目标部门" prop="dept" v-if="formData.type==3">
            <dept-select-tree v-model="formData.dept" multiple></dept-select-tree>
          </el-form-item>
            <el-form-item label="消息内容" prop="desc">
              <my-editor
                ref='editor'
                v-model="formData.desc"
                :disabled="disabled"
              ></my-editor>
            </el-form-item>
            <el-form-item label="上传附件" prop="files">
                <my-upload-file   accept="image/*" v-model="formData.files" previewOrDownload="download" :disabled="disabled"></my-upload-file>
            </el-form-item>
        </el-form>
    </base-dialog>
</template>
<script>
    import DetailDialog from "@/mixins/DetailDialog";
    import BaseDialog from "@/components/Common/BaseDialog";
    import DeptSelectTree from "@/components/Common/DeptSelectTree";
    import role from "@/api/systemManagement/role/index";
    import user from "@/api/systemManagement/user/index";
    import {handleTree} from "@/utils/common";
    import MyUploadFile from "@/components/Common/MyUploadFile";
    import UEditor from '../../../components/editor/UEditor'
    import DictRadio from '../../../components/DictRadio/index'
    import RoleSelect from '../../../components/Common/RoleSelect'
    import UserSelectSearch from '../../../components/Common/UserSelectSearch'
    import MyEditor from '../../../components/editor/MyEditor'
    export default {
        name: "DetailDialog",
        components: { MyEditor, UserSelectSearch, RoleSelect, DictRadio, UEditor, MyUploadFile, DeptSelectTree, BaseDialog},
        mixins: [DetailDialog],
        data() {
            return {
                deptsList: [],
                deptsAllList:[],
                roleList:[],
                formData: {
                    title: "",
                    desc: "",
                    type: 1,
                    files:"",
                  user:undefined,
                  role:undefined,
                  dept:undefined
                },
                rules: {
                    title: [{ required: true, message: "请输入标题", trigger: "blur" }],
                },
            }
        },
        computed: {
            title() {
                return this.disabled?'查看消息':this.formData.id?"编辑消息":"新增消息"
            },
        },
        created() {
            this.getAccountsRolesNotice_user()
        },
        methods: {
            getSimpleDeptList(val) {
                //console.log(val);
                user.getSimpleDeptList({name:val}).then(res => {
                    this.deptsAllList= handleTree(res.data, "id", "parent_dept");
                    this.deptsList = res.data.filter(item=>{
                        return item.parent_dept==null
                    });
                });
            },
            getAccountsRolesNotice_user(val){
              role.getSimpleList({name:val}).then(res=>{
                    this.roleList = res.data
                })
            },
        }
    }
</script>
